﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SimpleLibraryClasses;
using System.Collections.Generic;

namespace SimpleLibraryWebForm
{
    public partial class CheckOut : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Library lib = GetLibrary();
                lbMemberList.DataSource = lib.Members;
                lbMemberList.DataTextField = "DisplayName";
                lbMemberList.DataValueField = "MemberId";
                lbMemberList.DataBind();
                RebindBookList(lib);
            }

        }

        protected void lbMemberList_SelectedIndexChanged(object sender, EventArgs e)
        {
                Library lib = GetLibrary();
                //Get the member object from the Members collection where the ID equals the selected
                List<Member> foundMember = (from a in lib.Members where a.MemberId == lbMemberList.SelectedValue select a).ToList();
                RebindLoanList(foundMember[0]);
        }

        protected void btnCheckOut_Click(object sender, EventArgs e)
        {
            //Todo Add user friendly messages if user hasn't selected item from each list
            if (lbMemberList.SelectedIndex != -1 && lbBookList.SelectedIndex != -1)
            {
                //First load the library
                Library lib = GetLibrary();
                //Get an instance of the object that represents the selected member
                List<Member> foundMember = (from a in lib.Members where a.MemberId == lbMemberList.SelectedValue select a).ToList();
                //Get an instance of the object that represents the selected book
                List<Book> foundBook = (from b in lib.Items where b.ItemCode == lbBookList.SelectedValue select b as Book).ToList();
                lib.BorrowItem(foundBook[0], foundMember[0]);
                RebindBookList(lib);
                RebindLoanList(foundMember[0]);
                SaveLibrary(lib);
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            //Todo Add user friendly messages if user hasn't selected item from each list
            if (lbCurrentLoans.SelectedIndex != -1 && lbMemberList.SelectedIndex !=-1)
            {
                Library lib = GetLibrary();
                List<Member> foundMember = (from a in lib.Members where a.MemberId == lbMemberList.SelectedValue select a).ToList();
                List<Loan> foundLoan = (from b in foundMember[0].Loans where b.LoanItem.ItemCode == lbCurrentLoans.SelectedValue select b).ToList();
                lib.ReturnItem(foundLoan[0]);
                RebindBookList(lib);
                RebindLoanList(foundMember[0]);
                SaveLibrary(lib);
            }
        }

        protected void RebindLoanList(Member mem)
        {
            lbCurrentLoans.DataSource = from loan in mem.Loans
                                        select new { DisplayName = loan.LoanItem.DisplayName + ".  Due: " + loan.DueDate.ToShortDateString(), BindValue = loan.LoanItem.ItemCode };
            lbCurrentLoans.DataTextField = "DisplayName";
            lbCurrentLoans.DataValueField = "BindValue";
            lbCurrentLoans.DataBind();          
        }

        protected void RebindBookList(Library lib)
        {
            List<Book> books = (from a in lib.Items where a is Book &&
                         a.ItemStatus == ItemStatus.Available select a as Book).ToList();
            lbBookList.DataSource = books;
            lbBookList.DataTextField = "DisplayName";
            lbBookList.DataValueField = "ISBN";
            lbBookList.DataBind();
        }

        protected Library GetLibrary()
        {
            if (Application["lib"] == null || ((Library)Application["lib"]).Items.Count == 0)
            {
                SaveLibrary(Library.LoadLibrary(LibraryFileHandler.GetLibraryFileName()));
            }
            return (Library)Application["lib"];
        }

        protected void SaveLibrary(Library lib)
        {
            Application["lib"] = lib;
        }

    }
}
